package org.batatasframework.chart.columns;

import java.text.DecimalFormat;

import org.batatasframework.chart.graphics.ChartGraphic;


/**
 * 四则运算，包括百分比的运算
 * @author jackson
 *
 */
public abstract class ColumnValueArithmetic extends AbstractFormatableColumn<ArithmeticDecimal> implements ChartColumnValue{
	
	protected final DecimalFormat DEAULT_PERCENT_FORMAT = new DecimalFormat("0.00%");
	protected final DecimalFormat DEAULT_NUMBER_FORMAT = new DecimalFormat("0.####");
	
	private final int[] oneValue;
	private final int[] twoValue;
//	private int ordinal;
	private String unit;
//	
//	@Override
//	public ChartColumnValue initialize(OrdinalAllocatable allocator) {
//		this.ordinal = allocator.allocate();
//		return this;
//	}
//	
//	@Override
//	public int getOrdinal() {
//		return ordinal;
//	}

	@Override
	public ChartColumnValue setUnit(String unit) {
		this.unit = unit;
		return this;
	}
	
	@Override
	public String toString() {
		String value = super.toString();
		if(null != unit) value += "(" + unit + ")";
		return value;
	}
	protected ColumnValueArithmetic(String name, int oneValue, int twoValue) {
		this(name, new int[]{ oneValue }, new int[]{ twoValue });
	}

	protected ColumnValueArithmetic(String name, int[] oneValue, int[] twoValue) {
		super(name);
		this.format = defaultFormat();
		this.oneValue = oneValue;
		this.twoValue = twoValue;
	}
	
	public static ColumnValueArithmetic division(String name, int oneValue, int twoValue){
		return new RowColumnArithmeticDivision(name, oneValue, twoValue);
	}

	public static ColumnValueArithmetic division(String name, int[] oneValue, int[] twoValue){
		return new RowColumnArithmeticDivision(name, oneValue, twoValue);
	}

	public static ColumnValueArithmetic multiplication(String name, int oneValue, int twoValue){
		return new RowColumnArithmeticMultiplication(name, oneValue, twoValue);
	}

	public static ColumnValueArithmetic multiplication(String name, int[] oneValue, int[] twoValue){
		return new RowColumnArithmeticMultiplication(name, oneValue, twoValue);
	}

	public static ColumnValueArithmetic percent(String name, int oneValue, int twoValue){
		return new RowColumnArithmeticPercent(name, oneValue, twoValue);
	}

	public static ColumnValueArithmetic percent(String name, int[] oneValue, int[] twoValue){
		return new RowColumnArithmeticPercent(name, oneValue, twoValue);
	}
	
	public static ColumnValueArithmetic sum(String name, int...indexOfs){
		return new RowColumnArithmeticSum(name, indexOfs);
	}
	
	/** 默认的数字格式，可以在具体使用的时候重新指定格式化表达式 */
	protected DecimalFormat defaultFormat(){
		return DEAULT_NUMBER_FORMAT;
	}

	@Override
	public ChartColumnValue graphic(ChartGraphic... graphics) {
		for(ChartGraphic graphic : graphics){
			graphic.add(this);
		}
		return this;
	}
	
	@Override
	protected ArithmeticDecimal internalParseValue(Object[] row) {
		return new ArithmeticDecimal(valueOf(row, oneValue), valueOf(row, twoValue));
	}

	private double valueOf(Object[] row, int[] indexOfs){
		double sum = 0d;
		if(null != indexOfs){
			for(int indexOf : indexOfs){
				if(indexOf < row.length && row[indexOf] instanceof Number){
					sum += ((Number)row[indexOf]).doubleValue();
				}
			}
		}
		return sum;
	}
	
	@Override
	protected ArithmeticDecimal mergedValue(ArithmeticDecimal oldValue, ArithmeticDecimal newValue) {
		return oldValue.add(newValue);
	}
}

class RowColumnArithmeticSum extends ColumnValueArithmetic{

	protected RowColumnArithmeticSum(String name, int...indexOfs) {
		super(name, indexOfs, null);
	}

	@Override
	protected Object resolveValue(ArithmeticDecimal value) {
		return value.sum();
	}
}

class RowColumnArithmeticDivision extends ColumnValueArithmetic{

	protected RowColumnArithmeticDivision(String name, int oneValue, int twoValue) {
		super(name, oneValue, twoValue);
	}

	protected RowColumnArithmeticDivision(String name, int[] oneValue, int[] twoValue) {
		super(name, oneValue, twoValue);
	}
	
	@Override
	protected Object resolveValue(ArithmeticDecimal value) {
		return value.division();
	}
}

class RowColumnArithmeticMultiplication extends ColumnValueArithmetic{

	protected RowColumnArithmeticMultiplication(String name, int oneValue, int twoValue) {
		super(name, oneValue, twoValue);
	}

	protected RowColumnArithmeticMultiplication(String name, int[] oneValue, int[] twoValue) {
		super(name, oneValue, twoValue);
	}
	
	@Override
	protected Object resolveValue(ArithmeticDecimal value) {
		return value.multiplication();
	}
}

class RowColumnArithmeticPercent extends ColumnValueArithmetic{

	protected RowColumnArithmeticPercent(String name, int oneValue, int twoValue) {
		super(name, oneValue, twoValue);
	}

	protected RowColumnArithmeticPercent(String name, int[] oneValue, int[] twoValue) {
		super(name, oneValue, twoValue);
	}

	protected DecimalFormat defaultFormat(){
		return DEAULT_PERCENT_FORMAT;
	}

	@Override
	protected Object resolveValue(ArithmeticDecimal value) {
		return value.percent();
	}
}
